package com.liaoct.lct.fragment;

import java.util.List;
import java.util.Map;
import com.liaoct.lct.activity.R;
import com.liaoct.lct.adapter.ContactListViewAdapter;
import com.liaoct.lct.db.DataBaseManagement;
import com.liaoct.lct.listener.ContactSearchEditTextChangeListener;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 联系人列表展示Fragment
 * 
 * @author liaoct
 * 
 */
public class LinkManFragment extends Fragment {

	/**
	 * 展示View
	 */
	private View view;

	private List<Map<String, Object>> list;

	private ListView listView;

	private TextView contactCount;

	private String[] from = { "mobile_number", "name", "photo_id", "id" };

	private int[] to = { R.id.contactPhone, R.id.contactName,
			R.id.contactPhoto, R.id.contactId };

	private EditText contactSearch;

	private DataBaseManagement db;

	/**
	 * 创建视图
	 * 
	 * @return MileageFragment的视图
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.view = inflater.inflate(R.layout.fragment_linkman, container,
				false);
		db = DataBaseManagement.newInstance(this.getActivity());
		list = db.queryContactList(null);
		this.init();
		return view;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	private void init() {
		listView = (ListView) view.findViewById(R.id.linkmanListView);
		ContactListViewAdapter listAdapter = new ContactListViewAdapter(
				this.getActivity(), list, from, to);
		listView.setAdapter(listAdapter);
		contactCount = (TextView) view.findViewById(R.id.contactCount);
		contactCount.setText("共有" + list.size() + "人");
		contactSearch = (EditText) view.findViewById(R.id.contactSearch);
		contactSearch.addTextChangedListener(new ContactSearchEditTextChangeListener(this.getActivity(), this.listView));
	}
}
